<?php


namespace app\common\model;


use think\Db;
use think\Model;

class MemberModel extends Model
{
    protected $name = 'wdsxh_member';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;


    /**
     * @ApiTitle    (修改会员和企业信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/wdsxh/member/editMemberAndUnitInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="string", required=true, description="会员姓名")
     * @ApiParams   (name="mobile", type="string", required=true, description="联系电话")
     * @ApiParams   (name="birthplace", type="string", required=true, description="会员籍贯")
     * @ApiParams   (name="picture", type="string", required=false, description="会员照片")
     * @ApiParams   (name="level_id", type="string", required=false, description="商会职务")
     * @ApiParams   (name="company_post", type="string", required=false, description="公司职务")
     * @ApiParams   (name="society_post", type="string", required=false, description="社会职务")
     * @ApiParams   (name="introduction", type="string", required=false, description="会员简介")
     * @ApiParams   (name="cat_id", type="string", required=true, description="行业分类")
     * @ApiParams   (name="company_name", type="string", required=true, description="公司名称")
     * @ApiParams   (name="company_logo", type="string", required=false, description="公司LOGO")
     * @ApiParams   (name="company_introduce", type="string", required=true, description="公司介绍")
     * @ApiParams   (name="company_product", type="string", required=true, description="公司产品")
     * @ApiParams   (name="company_mobile", type="string", required=false, description="公司电话")
     * @ApiParams   (name="company_address", type="string", required=true, description="公司地址")
     * @ApiParams   (name="company_address_detail", type="string", required=true, description="公司详细地址")
     * @ApiParams   (name="company_address_lng", type="string", required=true, description="公司地址经纬度")
     * @ApiParams   (name="company_address_lat", type="string", required=true, description="公司地址经纬度")
     * @ApiParams   (name="company_site", type="string", required=false, description="公司网址")
     * @ApiParams   (name="company_email", type="string", required=false, description="公司邮箱")
     */
    public function editMemberAndUnitInfo($params, $user_id): array
    {
        extract($params);
        Db::startTrans();
        try {
            $member = model('app\admin\model\wdsxh\Member')->get(['uid' => $user_id,'status' => '1']);
            if (!$member) {
                throw new \Exception('非会员用户无法访问', 41001);
            }
            $unit = model('app\admin\model\wdsxh\MemberUnit')->where(['member_id' => $member['id'],'status' => '1'])->order('id desc')->find();
            if (!$unit) {
                throw new \Exception('会员单位信息不存在', 41002);
            }

            $member['name'] = $params['name'];
            $member->mobile = $params['mobile'];
            $member->birthplace = $params['birthplace'];
            $member->picture = $params['picture'];
            $member->level_id = $params['level_id'];
            $member->company_post = $params['company_post'];
            $member->society_post = $params['society_post'];
            $member->introduction = $params['introduction'];
            $member->save();

            $unit->cat_id = $params['cat_id'];
            $unit->company_name = $params['company_name'];
            $unit->company_logo = $params['company_logo'];
            $unit->company_introduce = $params['company_introduce'];
            $unit->company_product = $params['company_product'];
            $unit->company_mobile = $params['company_mobile'];
            $unit->company_address = $params['company_address'];
            $unit->company_address_detail = $params['company_address_detail'];
            $unit->company_address_lng = $params['company_address_lng'];
            $unit->company_address_lat = $params['company_address_lat'];
            $unit->company_site = $params['company_site'];
            $unit->company_email = $params['company_email'];
            $unit->save();

            Db::commit();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        } catch (\Exception $e) {
            Db::rollback();
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }

}